﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using zyconfig;

namespace zymain
{
    public class FileFunction
    {
        public static List<int> getBrokenNumber(int size,string tableName,string filedName)
        {
            List<int> nums = new List<int>();
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.SqlCmd = "select beginId,(select min("+filedName+")-1 from "+tableName+" where "+filedName+" > beginId) as endId from(select "+filedName+" + 1 as beginId from "+tableName+" where "+filedName+"+ 1 not in (select "+ filedName+" from "+ tableName+" ) and 主键码< (select max("+filedName+") from "+tableName+ " )) as t where beginId> 0 group by beginId  order by beginId";
                DataTable dr = _open.GetDataTable();
                int num = 0;
                foreach(DataRow drow in dr.Rows)
                {
                    int beginId = Convert.ToInt32(drow["beginId"]);
                    int endId = Convert.ToInt32(drow["endId"]);
                    for (int i = 0; i <= endId - beginId; i++)
                    {
                        nums.Add(beginId + i);
                        num++;
                        if (num == size)
                            break;
                    }
                    if (num == size)
                        break;
                }
            }
            return nums;
        }
        public static bool InsertInfor(string tableName,string MainKay, string KzName, string FileName, string UploadUser, string FilePath, string Guid)
        {
            bool flag;
            using (DbOperHandler _open = new Common().Doh())
            {
                string sql = string.Empty;
                switch (tableName)
                {
                    case "0":
                        _open.Reset();
                        _open.ConditionExpress = "主键码=" + MainKay+" and 完成状态=1";
                        int c = _open.Count("Disk_文件信息表");
                        if (c == 0)
                        {
                            sql = "Insert into Disk_文件信息表 (创建时间,创建人,完成状态,文件路径,文件名称,扩展名,主键码,GUID,顺序) values('" + DateTime.Now.ToString() + "'," + UploadUser + ",0,'" + FilePath + "','" + FileName + "','" + KzName + "'," + MainKay + ",'" + Guid + "',1)";
                            if (_open.dbType.ToString().Equals("ORACLE") || _open.odbType.ToString().Equals("ORACLE"))
                            {
                                sql = "Insert into Disk_文件信息表 (创建时间,创建人,完成状态,文件路径,文件名称,扩展名,主键码,GUID,编号,顺序) values('" + DateTime.Now.ToString() + "'," + UploadUser + ",0,'" + FilePath + "','" + FileName + "','" + KzName + "'," + MainKay + ",'" + Guid + "',DISK_文件信息表_编号.NEXTVAL,1)";
                            }
                        }
                        else
                        {
                            sql = "Insert into Disk_文件信息表 (创建时间,创建人,完成状态,文件路径,文件名称,扩展名,主键码,GUID,顺序) values('" + DateTime.Now.ToString() + "'," + UploadUser + ",0,'" + FilePath + "','" + FileName + "','" + KzName + "'," + MainKay + ",'" + Guid + "',"+ (c+1)+")";
                            if (_open.dbType.ToString().Equals("ORACLE") || _open.odbType.ToString().Equals("ORACLE"))
                            {
                                sql = "Insert into Disk_文件信息表 (创建时间,创建人,完成状态,文件路径,文件名称,扩展名,主键码,GUID,编号,顺序) values('" + DateTime.Now.ToString() + "'," + UploadUser + ",0,'" + FilePath + "','" + FileName + "','" + KzName + "'," + MainKay + ",'" + Guid + "',DISK_文件信息表_编号.NEXTVAL," + (c + 1) + ")";
                            }
                        }
                       
                        break;
                    case "1":
                        _open.Reset();
                        _open.ConditionExpress = "登录号='" + MainKay+ "' and 完成状态 = 1";
                        int fc = _open.Count("Disk_复本文件信息表");
                        if (fc == 0)
                        {
                            sql = "Insert into Disk_复本文件信息表 (创建时间,创建人,完成状态,文件路径,文件名称,,扩展名,登录号,GUID,顺序) values('" + DateTime.Now.ToString() + "'," + UploadUser + ",0,'" + FilePath + "','" + FileName + "','" + KzName + "'," + MainKay + ",'" + Guid + "',1)";
                            if (_open.dbType.ToString().Equals("ORACLE") || _open.odbType.ToString().Equals("ORACLE"))
                            {
                                sql = "Insert into Disk_复本文件信息表 (创建时间,创建人,完成状态,文件路径,文件名称,扩展名,登录号,GUID,编号,顺序) values('" + DateTime.Now.ToString() + "'," + UploadUser + ",0,'" + FilePath + "','" + FileName + "','" + KzName + "'," + MainKay + ",'" + Guid + "',DISK_复本文件信息表_编号.NEXTVAL,1)";
                            }
                        }
                        else
                        {
                            sql = "Insert into Disk_复本文件信息表 (创建时间,创建人,完成状态,文件路径,文件名称,,扩展名,登录号,GUID,顺序) values('" + DateTime.Now.ToString() + "'," + UploadUser + ",0,'" + FilePath + "','" + FileName + "','" + KzName + "'," + MainKay + ",'" + Guid + "'," + (fc + 1) + ")";
                            if (_open.dbType.ToString().Equals("ORACLE") || _open.odbType.ToString().Equals("ORACLE"))
                            {
                                sql = "Insert into Disk_复本文件信息表 (创建时间,创建人,完成状态,文件路径,文件名称,扩展名,登录号,GUID,编号,顺序) values('" + DateTime.Now.ToString() + "'," + UploadUser + ",0,'" + FilePath + "','" + FileName + "','" + KzName + "'," + MainKay + ",'" + Guid + "',DISK_复本文件信息表_编号.NEXTVAL," + (fc + 1) + ")";
                            }
                        }
                        break;
                    case "2":
                        _open.Reset();
                        _open.ConditionExpress = "主键码=" + MainKay + " and 完成状态=1";
                        int qc = _open.Count("DISK_情报检索文件信息表");
                        if (qc == 0)
                        {
                            sql = "Insert into DISK_情报检索文件信息表 (创建时间,创建人,完成状态,文件路径,文件名称,扩展名,主键码,GUID,顺序) values('" + DateTime.Now.ToString() + "'," + UploadUser + ",0,'" + FilePath + "','" + FileName + "','" + KzName + "'," + MainKay + ",'" + Guid + "',1)";
                            if (_open.dbType.ToString().Equals("ORACLE") || _open.odbType.ToString().Equals("ORACLE"))
                            {
                                sql = "Insert into DISK_情报文件信息表 (创建时间,创建人,完成状态,文件路径,文件名称,扩展名,主键码,GUID,编号,顺序) values('" + DateTime.Now.ToString() + "'," + UploadUser + ",0,'" + FilePath + "','" + FileName + "','" + KzName + "'," + MainKay + ",'" + Guid + "',DISK_情报检索文件信息表_编号.NEXTVAL,1)";
                            }
                        }
                        else
                        {
                            sql = "Insert into DISK_情报检索文件信息表 (创建时间,创建人,完成状态,文件路径,文件名称,扩展名,主键码,GUID,顺序) values('" + DateTime.Now.ToString() + "'," + UploadUser + ",0,'" + FilePath + "','" + FileName + "','" + KzName + "'," + MainKay + ",'" + Guid + "'," + (qc + 1) + ")";
                            if (_open.dbType.ToString().Equals("ORACLE") || _open.odbType.ToString().Equals("ORACLE"))
                            {
                                sql = "Insert into DISK_情报文件信息表 (创建时间,创建人,完成状态,文件路径,文件名称,扩展名,主键码,GUID,编号,顺序) values('" + DateTime.Now.ToString() + "'," + UploadUser + ",0,'" + FilePath + "','" + FileName + "','" + KzName + "'," + MainKay + ",'" + Guid + "',DISK_情报检索文件信息表_编号.NEXTVAL," + (qc + 1) + ")";
                            }
                        }
                        break;
                }
                try
                {
                    _open.SqlCmd = sql;
                    _open.ExecuteSqlNonQuery();
                    flag = true;
                }
                catch(OleDbException ex)
                {
                    //Console.WriteLine(sql);
                    Console.WriteLine(ex.Message);
                    flag = false;
                }
            }
            return flag;
        }

        public static string GetMainKay(string tableName,string Guid, out string FilePath)
        {
            string str4 = ""; FilePath="";
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.ConditionExpress = " GUID='" + Guid + "' and 完成状态= 1";
                switch (tableName)
                {
                    case "0":
                        FilePath = _open.GetField("Disk_文件信息表", "文件路径").ToString().Trim();
                        str4 = _open.GetField("Disk_文件信息表", "主键码").ToString().Trim();
                        break;
                    case "1":
                        FilePath = _open.GetField("Disk_复本文件信息表", "文件路径").ToString().Trim();
                        str4 = _open.GetField("Disk_复本文件信息表", "登录号").ToString().Trim();
                        break;
                    case "2":
                        FilePath = _open.GetField("DISK_情报检索文件信息表", "文件路径").ToString().Trim();
                        str4 = _open.GetField("DISK_情报检索文件信息表", "主键码").ToString().Trim();
                        break;
                }
            }
            return str4;
        }

        public static bool UpdateSucess(string tableName ,string Guid, string UserID)
        {
            bool flag;
            using (DbOperHandler _open = new Common().Doh())
            {
                string str = string.Empty;
                switch (tableName)
                {
                    case "0":
                        str = "update Disk_文件信息表 set 完成状态= 1,上传人=" + UserID + ",上传时间='" + DateTime.Now.ToString() + "' where GUID='" + Guid + "' and 完成状态= 0";
                        break;
                    case "1":
                        str = "update Disk_复本文件信息表 set 完成状态= 1,上传人=" + UserID + ",上传时间='" + DateTime.Now.ToString() + "' where GUID='" + Guid + "' and 完成状态= 0";
                        break;
                    case "2":
                        str = "update DISK_情报检索文件信息表 set 完成状态= 1,上传人=" + UserID + ",上传时间='" + DateTime.Now.ToString() + "' where GUID='" + Guid + "' and 完成状态= 0";
                        break;
                }
                _open.SqlCmd = str;
                try
                {
                    _open.ExecuteSqlNonQuery();
                    flag = true;
                }
                catch { flag = false; }
            }
            return flag;
        }
    }
}
